// 订单状态
const formatOrderStatus = order => {
  if (order.orderStatus === 0) {
    order.orderStatus = 0;
    order.orderCnStatus = "待付款";
  } else if (order.orderStatus === 1) {
    order.orderStatus = 1;
    order.orderCnStatus = "待拼团";
  } else if (order.orderStatus === 2) {
    order.orderStatus = 2;
    order.orderCnStatus = "待发货";
  } else if (order.orderStatus === 3) {
    order.orderStatus = 3;
    order.orderCnStatus = "待收货";
  } else if (order.orderStatus === 4) {
    order.orderStatus = 4;
    order.orderCnStatus = "待评价";
  }
  return order;
}

// 授权
const auth = (scope, callback) => {
  wx.getSetting({
    success: res => {
      if (!res.authSetting[scope]) {
        if (scope == "scope.userInfo") {
          callback()
          return;
        }
        wx.authorize({
          scope: scope,
          success() {
            callback()
          },
          fail() {
            let content = "";
            if (scope == "scope.writePhotosAlbum") {
              content = "需要您授权保存到相册的权限，请到小程序的设置中打开授权";
            } else if (scope == "scope.userLocation") {
              content = "需要您授权获取地理位置的权限，请到小程序的设置中打开授权";
            } else if (scope == "scope.userInfo") {
              content = "需要您授权获取用户信息的权限，请到小程序的设置中打开授权";
            }
            wx.showModal({
              title: '是否要打开设置页面',
              content: content,
              success: function(res) {
                if (res.confirm) {
                  wx.openSetting({
                    success(res) {
                      if (res.authSetting[scope]) {
                        callback()
                      } else {
                        wx.showToast({
                          title: "未获得用户授权,无法使用相应功能",
                          icon: 'none',
                          duration: 2000
                        });
                      }
                    }
                  });
                } else {
                  wx.showToast({
                    title: "未获得用户授权,无法使用相应功能",
                    icon: 'none',
                    duration: 2000
                  });
                }
              }
            })
          }
        })
      } else {
        callback()
      }
    }
  })
}

module.exports = {
    formatOrderStatus:formatOrderStatus,
    auth:auth
}